package com.warrior.gatherclient.service.impl;

import com.warrior.gatherclient.grpc.Messages;
import com.warrior.gatherclient.service.OSProcessService;
import com.warrior.gatherclient.util.CommonUtil;
import com.warrior.gatherclient.service.SystemInfoService;
import com.warrior.gathercommon.constants.CommonStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

/**
 * @ClassName com.warrior.gatherclient.service.impl.OSProcessServceiImpl.java
 * @Author xujian
 * @Description
 * @CreateTime 2020年12月16日 15:50:00
 */
@Slf4j
@Service
public class OSProcessServceImpl implements OSProcessService {

    @Autowired
    private SystemInfoService systemInfoService;

    @Override
    public String getServiceKey() {
        return CommonStatus.CONSTANT_OSPROCESS;
    }

    @Override
    public Messages.Builder execute() {
        SystemInfo systemInfo = systemInfoService.getSystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        int processId = operatingSystem.getProcessId();
        OSProcess process = operatingSystem.getProcess(processId);
        long openFiles = process.getOpenFiles();

        Messages.Builder messages = CommonUtil.initMessage(CommonStatus.CONSTANT_OSPROCESS);
        //操作系统版本
        Messages.Metric.Builder builder = Messages.Metric.newBuilder();
        builder.setMetricName(CommonStatus.CONSTANT_OSPROCESS_FILES);
        builder.setMetricVaule(String.valueOf(openFiles));
        messages.addMetrics(builder.build());

        return messages;
    }
}
